/**
 * 
 */
package au.com.betaversion.service.fileload;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import au.com.betaversion.domain.Aircraft;
import au.com.bytecode.opencsv.CSVReader;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;

/**
 * @author Home
 *
 */
public class FileLoaderUSA implements FileLoader {
	
	private static final Logger logger = LoggerFactory.getLogger(FileLoaderUSA.class);
	
	private class Manufacturer implements Comparable<Manufacturer> {
		
		private String code;
		private String manufacturer;
		private String model;
		
		public String getManufacturer() {
			return manufacturer;
		}
		public void setManufacturer(String manufacturer) {
			this.manufacturer = manufacturer;
		}
		public String getModel() {
			return model;
		}
		public void setModel(String model) {
			this.model = model;
		}
		public String getCode() {
			return code;
		}
		public void setCode(String code) {
			this.code = code;
		}
		@Override
		public int compareTo(Manufacturer o) {
			return this.code.compareTo(o.getCode());
		}
	}

	/* (non-Javadoc)
	 * @see au.com.betaversion.service.fileload.FileLoader#loadFileToCollectio(com.mongodb.DBCollection)
	 */
	@Override
	public void loadFileToCollectio(DBCollection collection) {
		File master = null;
		File reference = null;
		CSVReader csvReader = null;
		
		Map<String, Aircraft> aircrafts = new HashMap<String, Aircraft>();
		Map<String, Manufacturer> manufacturers = new HashMap<String, Manufacturer>();
		
		try {
			String [] nextLine;

		    reference = new File("C:/Users/Home/Downloads/AR022013/ACFTREF.TXT");
		    csvReader = new CSVReader(new FileReader(reference), ',');
		    while ((nextLine = csvReader.readNext()) != null) {
		    	Manufacturer manufacturer = new Manufacturer();
		    	manufacturer.setCode(nextLine[0].trim());
		    	manufacturer.setManufacturer(nextLine[1].trim());
		    	manufacturer.setModel(nextLine[2].trim());
		    	manufacturers.put(manufacturer.getCode(), manufacturer);
		    }
			
			master = new File("C:/Users/Home/Downloads/AR022013/MASTER.TXT");
			csvReader = new CSVReader(new FileReader(master), ',');
		    while ((nextLine = csvReader.readNext()) != null) {
		    	Aircraft aircraft = new Aircraft();
		    	aircraft.setMark("N-" + nextLine[0].trim());
		    	aircraft.setSerialNumber(nextLine[1].trim());
	    		aircraft.setOperatorState(nextLine[10].trim());
	    		aircraft.setOperatorName(nextLine[6].trim());
	    		
	    		Manufacturer manufacturer = manufacturers.get(nextLine[2].trim());
	    		
	    		if(manufacturer != null) {
	    			aircraft.setManufacturer(manufacturer.getManufacturer());
	    			aircraft.setModel(manufacturer.getModel());
	    		}
	    		
		    	aircrafts.put("N-" + nextLine[0].trim(), aircraft);

		    	DBObject object = new BasicDBObject();
	    		object.put("_id", aircraft.getMark());
	    		object.put("manufacturer", aircraft.getManufacturer());
	    		object.put("model", aircraft.getModel());
	    		object.put("serialNumber", aircraft.getSerialNumber());
	    		object.put("operatorName", aircraft.getOperatorName());
	    		object.put("operatorState", aircraft.getOperatorState());
	    		collection.save(object);
		    }
		} catch (ArrayIndexOutOfBoundsException e) {
			logger.error(e.getMessage());
		} catch (FileNotFoundException e) {
			logger.error(e.getMessage());
		} catch (IOException e) {
			logger.error(e.getMessage());
		} finally {
			try {
				csvReader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void main(String[] args) {
		FileLoader fileLoader = new FileLoaderUSA();
		MongoDbHelper dbHelper = new MongoDbHelper();

	    if(!dbHelper.authenticate()) {
	    	System.out.println("Authentication failed");
	    }
		
		DBCollection collection = dbHelper.getCollection("aircraft");
		System.out.println("Count before: " + collection.count());
		System.out.println("Working on 'aircraft' collection");
		
		fileLoader.loadFileToCollectio(collection);
		System.out.println("Count after: " + collection.count());
	}

}
